
{extend name='public/base' /}

{block name="content"}

<div class="layui-col-md12">
    <div class="layui-card">
        <div class="layui-card-body">
            <form action="" class="layui-form" method="get">
                <div class="layui-form-item layui-form-pane">
                    <div class="layui-inline">
                        <label class="layui-form-label">商户：</label>
                        <div class="layui-input-inline">
                            <select name="merchant_id" lay-verify="required" lay-search="" id="merchant_id">
                                <option value="0" ></option>
                                {volist name="merchant_list" id="t"}
                                {if empty($t.name)||$t.name=='-'}
                                <option value="{$t.id}" >{$t.company} - {$t.code}-{$t.biz_state}-{$t.positions_code}</option>
                                {else/}
                                <option value="{$t.id}" >{$t.name} - {$t.code}-{$t.biz_state}-{$t.positions_code}</option>
                                {/if}
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="overflow: inherit;">合同号：</label>
                        <div class="layui-input-inline">
                            <input name="contract_code" class="layui-input" id="contract_code" value="" type="text"
                                   placeholder="合同号">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="overflow: inherit;">表号：</label>
                        <div class="layui-input-inline">
                            <input name="pointId" class="layui-input" id="pointId" value="" type="text"
                                   placeholder="请输入表号">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">支付状态：</label>
                        <div class="layui-input-inline">
                            <select name="pay_status" id="pay_status" >
                                <option value="" >请选择</option>
                                <option value="0" >未支付</option>
                                <option value="1" >已支付</option>
                            </select>
                        </div>
                    </div>
<!--                    &lt;!&ndash; 新增的开票状态 &ndash;&gt;-->
                    <div class="layui-inline">
                        <label class="layui-form-label">开票状态：</label>
                        <div class="layui-input-inline">
                            <select name="invoice" id="invoice">
                                <option value="" >请选择</option>
                                <option value="0" >未开票</option>
                                <option value="1" >已开票</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">请选择结算期</label>
                        <div class="layui-inline" id="test6">
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" name="settlement_time" id="settlement_time" class="layui-input" placeholder="开始日期">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn layui-btn-sm" data-id="table_id" lay-filter="search" lay-submit="">
                            <i class="layui-icon"></i>搜索
                        </button>
                        <button type="reset" class="layui-btn icon-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-refresh"></i>{:__('Reset')}</button>
                        <button class="{:node('WaterLog/export')} layui-btn layui-bg-black iconfont icon-daochu layui-btn-sm" lay-submit id="export" >导出</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" id="table_id" lay-filter="table_id"></table>
        </div>
    </div>

</div>
{/block}

{block name="js"} <!--js处理区-->


<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button type="button" lay-event="add"  class="layui-btn {:node('WaterLog/add')} layui-btn-sm">生成费用单</button>
        <button type="button" lay-event="reload"  class="layui-btn  layui-btn-sm  layui-btn-primary">刷新表格</button>
        <button type="button" lay-event="invoceAll"  class="layui-btn  layui-btn-sm  layui-btn-primary">批量开票</button>
        <a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-sm {:node('WaterLog/del')}"  data-title="删除" lay-event="del">批量删除</i></a>
    </div>
</script>


<script type="text/html" id="operationTpl">
    {{# if(d.pay_status==3){ }}
<!--    <a href="javascript:;" class="layui-btn  layui-btn-xs {:node('Cost/costEdit')}" data-title="审核" lay-event="edit"><i class="layui-icon">&#xe679;</i></a>-->
    {{# }else{ }}
<!--    <a  class="layui-btn  layui-btn-xs {:node('Cost/costEdit')}  layui-btn-disabled" data-title="审核" ><i class="layui-icon">&#xe679;</i></a>-->
    {{# } }}
    {{# if(d.pay_status==1 || d.pay_status == -1){ }}
    <a href="javascript:;" class="layui-btn   layui-btn-xs {:node('common/costPrint')}" data-title="打印" lay-event="print"><i class="layui-icon">&#xe66d;</i></a>
    {{# }else{ }}
    <a  class="layui-btn layui-btn-disabled layui-btn-xs {:node('common/costPrint')}"  data-title="打印" ><i class="layui-icon">&#xe66d;</i></a>
    {{# } }}
    <a href="javascript:;" class="layui-btn  layui-btn-primary layui-btn-xs {:node('WaterLog/detail')}" data-title="查看详情" lay-event="detail"><i class="layui-icon">&#xe705;</i></a>
    <a href="javascript:;" class="layui-btn  layui-btn-danger layui-btn-xs {:node('WaterLog/del')}" data-title="删除" lay-event="del"><i class="layui-icon"></i></a>
</script>

<script>
    layui.use(['element', 'table', 'form', 'jquery', 'lucky','opTable','laydate'], function () {
        var element = layui.element;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        var lucky=layui.lucky;
        var opTable = layui.opTable;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#settlement_time'
            ,type: 'month',
            trigger: 'click'
        });
        form.render();
        var myTables = table.render({
            elem: '#table_id',
            url:"{:url('index')}",
            where: { info_type: 2},
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            even: true, //开启隔行背景
            id:'table_id',
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip','last'] //自定义分页布局
                ,groups: 5 //只显示 1 个连续页码
                ,first: false //不显示首页
                ,last: false //不显示尾页
                ,limit:20
                ,limits:[10,20,30,50]
            },
            // height: 'full-100',100',
            text: {
                none: '暂无相关数据'
            },
            cols: [[
                { title: '', type: 'checkbox', fixed: true},
                { title: '品牌名',width:100, field: 'merchant_user',templet:function (item) {
                        // +'('+item.merchant.mobile+')'
                        return item.merchant.name;
                    }, fixed:'left'},
                { title: '签约租户', width:200,field: 'merchant',templet:function (item) { return item.merchant.company}},
                { title: '合同号', width:200, field: 'merchant_user',templet:function (item) {
                        // +'('+item.merchant.mobile+')'
                        return item.merchant.code;
                    }},
                {title: '表号', field: 'pointId',width:200, templet: function (item) {
                        return item.pointId
                    }
                },
                { title: '类型',width:'10%',  field: 'electrity_type',templet:function (item){
                        return '单一水价'
                    }},
                { title: '实际用量',width:'10%',  field: 'use_number'},
                // { title: '水费费(元)',width:'10%',  field: 'amount',templet:function (item) { return item.amount/100} },
                // { title: '税费(元)',width:'10%',  field: 'service_charge',templet:function (item) { return item.service_charge/100} },
                { title: '合计(元)',width:'10%',  field: 'all_amount',templet:function (item) { return item.all_amount/100} },
                { title: '结算期',width:100,  field: 'settlement_time'},
                { title: '创建时间',width:'20%', field: 'create_time',templet:function (item){return layui.util.toDateString((item.create_time)*1000)}},
                { title: '缴费状态',width:100,  field: 'pay_status',templet:function (item) {
                        var status = item.pay_status
                        if ( status== 0){
                            return '未支付'
                        } else if (status == 1){
                            return '已支付'
                        } else if (status == -1){
                            return '费用'
                        }else if (status == 3){
                            return '待审核'
                        } else{
                            return ''
                        }
                    }},
                { title: '缴费时间',width:160, field: 'pay_time',align:'center',templet:function (item){
                        if(item.pay_time){

                            return item.pay_time;
                        }else{
                            return '未缴费';
                        }
                    }},
                { title: '打印次数', field: 'print_number' , edit: 'text',width:90},
                {
                    title: '开票状态',width:100,  field: 'invoice', fixed:'right', templet: function (item) {
                        if (item.invoice == 0){
                            return  '<div  class="layui-btn layui-btn-sm layui-btn-danger " lay-event="invoice">未开票</div>';
                            // return '未开票';
                        } else if (item.invoice == 1){
                            return  '<div  class="layui-btn layui-btn-sm layui-btn-primary "  lay-event="invoice">已开票</div>';
                            // return '已开票';
                        }
                    }
                },
                {templet: '#operationTpl', width: 240,align: 'center', title: '操作', fixed:'right'}
            ]],
            //  展开的列配置
            openCols: [
                {field: 'desc', title: '描述信息'}
            ],
            done: function (res) {
                layer.closeAll('loading');
            }
        });

//导出事件
        $('#export').on('click',function () {
            var merchant_id = $('#merchant_id').val() ;
            var contract_code = $('#contract_code').val() ;
            var pointId = $('#pointId').val() ;
            var pay_status = $('#pay_status').val() ;
            var settlement_time = $('#settlement_time').val() ;
            var url = "{:url('export')}"+'?pay_status='+pay_status+ "&pointId=" + pointId + "&export=" + 1+ "&merchant_id=" + merchant_id+ "&contract_code=" + contract_code+ "&settlement_time=" + settlement_time;
            window.open(url);
        });

        //导出时阻止页面刷新
        $(".layui-form").submit(function (e) {
            e.preventDefault();
        })
        /**
         * 监听单行工具操作
         */
        table.on('tool(table_id)', function (obj) {
            var data = obj.data;
            var _id=parseInt(data.id);
            var layEvent = obj.event;
            if(layEvent==="detail"){
                lucky.CreateOpenForm("查看","{:url('detail')}?merchant_id=" + data.merchant_id+"&type="+data.electrity_type+"&settlement_time="+data.settlement_time+'&merchant_sn='+data.merchant.merchant_sn+'&pointId='+data.pointId);
                return false;
            }else if(layEvent==="del"){
                lucky.FormatData(_id,"{:url('del')}");
            }else if(layEvent==="edit"){
                lucky.CreateOpenForm("审核提交","{:url('common/costEdit')}?id=" + _id);
                return false;
            }else if(layEvent==="print"){
                lucky.CreateOpenForm("打印","{:url('common/costPrint')}?id=" + _id,'85%');
                return false;
            }else if(layEvent==="invoice"){
                var datas = {} ;
                datas.id      = _id ;   //  购买数量
                $.ajax({
                    url:"{:url('invoice')}",
                    type:"post",
                    data: datas,
                    dataType:"json",
                    success:function(data){
                        lucky.layerMsg(data.msg, 1, function() {
                            setTimeout(function() {
                                lucky.CreateReload(table_id); //重载表格数据
                            }, 500);
                        });
                    }
                });
                return false;
            }
        });
        table.on('toolbar(table_id)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if(obj.event==="add"){
                lucky.CreateOpenForm("生成费用单","{:url('add')}");
                return false;
                // adminAdd
            } else if(obj.event==="adminAdd"){
                lucky.CreateOpenForm("添加人工缴费用","{:url('adminAdd')}");
                return false;
            }else if(obj.event==="close"){
                opTables.closeAll();
            }else if(obj.event==="open"){
                opTables.openAll();
            }else if(obj.event==="detail"){
                lucky.CreateOpenForm("订单详情","{:url('detail')}");
                return false;
            }else if(obj.event==="reload"){
                lucky.CreateReload();
            }else if(obj.event==="del"){
                var num=0;
                var id=[];
                for (var i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (num<1) {
                    layer.msg("请选择一项",{time:1500});return false;
                }
                lucky.FormatData(id,"{:url('del')}");
                return  false;
            }else if(obj.event==="invoceAll"){
                var num=0;
                var id=[];
                for (var i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (num<1) {
                    layer.msg("请选择一项",{time:1500});return false;
                }
                lucky.FormatData(id,"{:url('invoceAll')}",'确认批量设置已开票');
                return  false;
            }
        });

        /**
         * 关键词搜索树
         */
        form.on('submit(search)', function (obj) {
            lucky.CreateSearch(obj.field);
            return false;
        });
    });

</script >

{/block}
